package com.fulin.leetcode;

/**
 * TODO
 *
 * @Author fulin
 * @Date 2022/9/22 16:27
 **/
public class Num160_相交的链表 {

    /**
     * 双指针法，解法很精妙,理解两个指针一定会相等是关键
     * @param headA
     * @param headB
     * @return
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode p1 = headA,p2 = headB;
        while(p1 != p2){
            p1 = p1 == null ? headB : p1.next;
            p2 = p2 == null ? headA : p2.next;
        }
        return p1;
    }
}
